/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package marcin.belzowski.geoMap;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.hibernate.ejb.QueryImpl;
/**
 *
 * @author marcin
 */
public class Main {
 

    public void findAll() {
         EntityManagerFactory emf = Persistence.createEntityManagerFactory("PZ2");
    EntityManager em = emf.createEntityManager();
        System.out.println(em);
        Query q = em.createNamedQuery("Position.findAll");
        em.clear();
        Query m = em.createQuery("select c from City c");
 org.hibernate.ejb.QueryImpl hs = (QueryImpl) q;

        org.hibernate.Query hbQuery = hs.getHibernateQuery();
        List<Position> list = q.getResultList();
        
//        List l = m.getResultList();
        
        
       System.out.println(list);

    }

    void findAll2() {
 EntityManagerFactory emf = Persistence.createEntityManagerFactory("PZ2");
    EntityManager em = emf.createEntityManager();
        Query q = em.createQuery("select count(ct.streetlaye) from Position p, City ct where true = crosses(makeline" +
                "(translate(p.point,-1,-1),translate( p.point,1,1) )" +
                "" +
                ",ct.theGeom)  group by ct.streetlaye order by ct.streetlaye");
//      Query q = em.createQuery("select c.positions from Car c" );
        org.hibernate.ejb.QueryImpl hs = (QueryImpl) q;
        org.hibernate.Query hbQuery = hs.getHibernateQuery();
        //liczba samochodów na ulicy street
        Query q2 = em.createQuery("select count(ct.streetlaye) from Position p, City ct where true = crosses(makeline" +
                "(translate(p.point,-1,-1),translate( p.point,1,1) )" +
                "" +
                ",ct.theGeom) and ct.streetlaye = :streetName group by ct.streetlaye ");

        Query minTime = em.createQuery("select cast(age(max(p.datesent),min(p.datesent)) as text) from Position p, City ct where true = crosses(makeline" +
                "(translate(p.point,-1,-1),translate( p.point,1,1) )" +
                "" +
                ",ct.theGeom) and ct.streetlaye = :streetName");
        //czas pojawienia się 1 samochodu na ulicy
         Query minTime2 = em.createQuery("select p from Position p, City ct where true = crosses(makeline" +
                "(translate(p.point,-1,-1),translate( p.point,1,1) )" +
                "" +
                ",ct.theGeom) and ct.streetlaye = :streetName and p.datesent = (select min(s.datesent) from Position s) ");
        minTime.setParameter("streetName", "ulica1");

        q2.setParameter("streetName", "ulica1");
        List<String> list = minTime.getResultList();
        List<Long> cl = q2.getResultList();
System.out.println(cl);
//2010-02-13 02:09:20.0

Object[] tab = list.toArray();
Calendar date = new GregorianCalendar();

System.out.println(list);
for (int i =0 ; i< list.size(); i++) {


       
}



//System.out.println(tab[i]+" "+ list.size());
    }

    public static void main(String[] args) {
//        new Main().findAll();
        System.out.println(new Date());
//        new Main().findAll2();
    }
}
